package com.ahwei.user.controller;


import com.ahwei.common.rest.base.R;
import com.ahwei.common.rest.base.ResultCode;
import com.ahwei.common.rest.exception.ApplicationException;
import com.ahwei.common.rest.exception.ResourceNotFoundException;
import com.ahwei.user.api.dto.UserDto;
import com.ahwei.user.convert.UserConvert;
import com.ahwei.user.entity.User;
import com.ahwei.user.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ahwei
 * @since 2022-05-31
 */
@RestController
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    private UserService userService;


    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Operation(summary = "通过ID查询单条数据", description = "通过ID查询单条数据", parameters = {
            @Parameter(name = "id", description = "唯一标识", in = ParameterIn.QUERY, schema = @Schema(implementation = Integer.class))})
    @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "操作成功", content = {@Content(mediaType = "application/json", schema = @Schema(implementation = R.class))}), @ApiResponse(responseCode = "404", description = "未找到相关信息",content = {@Content(mediaType = "application/json", schema = @Schema(implementation = R.class))})})
    @GetMapping("/{id}")
    public R<UserDto> getById(@PathVariable("id") Long id) {
        User user = userService.getById(id);
        // 如果没有数据，就抛异常没有找到资源
        if (null != user) {
            UserDto userDto = UserConvert.INSTANCE.entity2dto(user);
            return R.ok(userDto);
        }

        throw new ResourceNotFoundException("未找到相关信息！");
    }
    /**
     * 新增数据
     *
     * @return 新增结果
     */
    @PostMapping()
    @Operation(summary = "新增数据", description = "新增数据", parameters = {
            @Parameter(name = "user", description = "封装信息", in = ParameterIn.QUERY, schema = @Schema(implementation = User.class))})
    @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "操作成功", content = {@Content(mediaType = "application/json",schema = @Schema(implementation = R.class))}),})
    @ResponseStatus(HttpStatus.CREATED)
    public R<Object> save(@Valid @RequestBody UserDto userDto) {
        // 将UserDto转为User
        User user = UserConvert.INSTANCE.dto2entity(userDto);
        boolean save = userService.save(user);
        if (save){
            return R.ok(ResultCode.SUCCESS,"保存成功！");
        }
        throw new ApplicationException("保存失败！");
    }

    /**
     * 更新数据
     *
     * @return 更新结果
     */
    @PutMapping()
    @Operation(summary = "更新数据", description = "更新数据", parameters = {
            @Parameter(name = "user", description = "更新信息", in = ParameterIn.QUERY, schema = @Schema(implementation = User.class))})
    @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "操作成功", content = {@Content(mediaType = "application/json", schema = @Schema(implementation = R.class))}),})
    @ResponseStatus(HttpStatus.OK)
    public R<Object> update(@Valid @RequestBody UserDto userDto) {
        //将UserDto转为User
        User user = UserConvert.INSTANCE.dto2entity(userDto);

        boolean update = userService.updateById(user);
        if (update){
            return R.ok(ResultCode.SUCCESS,"更新成功！");
        }
        throw new ApplicationException("更新失败！");
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除数据", description = "删除数据", parameters = {
            @Parameter(name = "id", description = "唯一标识", in = ParameterIn.QUERY, schema = @Schema(implementation = List.class))})
    public R<Object> remove(@PathVariable("id") List<Long> id) {
        boolean remove = userService.removeByIds(id);
        if (remove) {
            return R.ok(ResultCode.SUCCESS,"删除成功！");
        }
        throw new ApplicationException("删除失败！");
    }
}

